<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Controller\store;

use App\Dao\ScanOrderRefundsDao;
use App\Request\Store\Order\ScanOrderRefunds;
use App\Request\Store\Order\ScanOrderRefundsRequest;
use App\Service\Store\ScanOrderService;
use App\Middleware\User\StoreMiddleware;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 扫码订单服务
 *
 * @AutoController()
 * @Middleware(StoreMiddleware::class)
 * @author 张昊(2396675557@qq.com)
 * @package App\Controller\user
 */
class ScanOrderController extends Controller
{

    /**
     * @Inject
     * @var ScanOrderService
     */
    public $scan_order_service;

    /**
     * @Inject
     * @var ScanOrderRefundsDao
     */
    public $scan_order_refunds_dao;

    /**
     * 订单列表
     */
    public function list()
    {
        $result = $this->scan_order_service->list([
            'status' => 1
        ]);

        $this->success($result);
    }

    /**
     * 退款订单
     */
    public function refund()
    {
        $result = $this->scan_order_service->list([
            'status' => 3
        ]);

        $this->success($result);
    }

    /**
     * 提交退款
     * @param ScanOrderRefundsRequest $request
     */
    public function create(ScanOrderRefundsRequest $request)
    {
        $params = $request->all();

        $this->scan_order_refunds_dao->create([
            'status' => 1,
            'user_id' => $params['user_id'],
            'order_no' => $params['order_no'],
            'title' => $params['title'],
            'admin_id' => storeId(),
            'created_at' => time(),
            'updated_at' => time(),
        ]);

        $this->scan_order_service->refunds($params['order_no']);

        $this->scan_order_service->scan_order_dao->update([
            'order_no' => $params['order_no'],
        ],[
            'status' => 3
        ]);

        $this->success();
    }
}